package com.android.aircondition.activity;

import java.util.ArrayList;

import android.R.color;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;

import com.android.aircondition.R;
import com.android.aircondition.common.ScoreGestureDetector;
import com.android.aircondition.service.GreeService;
import com.android.aircondition.service.Mode;
import com.android.aircondition.service.Temperature;
import com.android.aircondition.service.Windy;
import com.android.aircondition.view.ModeChushiView;
import com.android.aircondition.view.ModeSongfengView;
import com.android.aircondition.view.ModeZhilengView;
import com.android.aircondition.view.ModeZhireView;
import com.android.aircondition.view.SubTabModeView;

public class HomeActivity extends BaseActivity {
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        super.setCommonLogo(this, R.drawable.logo);
    }

    @Override
    protected void onResume() {
    	super.onResume();
    	
    	setViews();
    	initDirection();

    }
    
    private void setViews(){
    	GreeService service = new GreeService(this);
    	View root = (View)findViewById(R.id.modeLayout);
    	SubTabModeView tab  = (SubTabModeView)findViewById(R.id.subTabModeView);
    	tab.setRootParent(root);
    	
    	resetViews();

    	final GestureDetector gestureDetectorScore = new GestureDetector(new ScoreGestureDetector(this,tab));
    	OnTouchListener gestureListenerScore = new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {

				if (gestureDetectorScore.onTouchEvent(event)) {
	                return true;
	            }
				
				return false;
			}
		};
		
		root.setOnTouchListener(gestureListenerScore);
		root.setOnLongClickListener(new OnLongClickListener() {
			public boolean onLongClick(View v) {
				return false;
			}
		});
    	
        ProgressBar   progressBar = (ProgressBar)findViewById(R.id.temperatureProgressBar);
        Windy windy  = service.getWindyPreference();
        progressBar.setProgress(50);
        
        
        final ImageView windImv = (ImageView)findViewById(R.id.windImageView); 
        final Animation windAnimation = new RotateAnimation ( 0, 360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        windAnimation.setDuration ( 5000 - progressBar.getProgress()*45);
        windAnimation.setRepeatCount(Animation.INFINITE);
        windAnimation.setInterpolator(new LinearInterpolator());
        windImv. setAnimation (windAnimation);
        windAnimation. startNow ();
        
        setTemperature(service.getModePreference());
        
        
}

	private void initDirection() {

		 WindowManager windowManager = getWindowManager();

		 Display display = windowManager.getDefaultDisplay();
		 int childWidth = display.getWidth()/5;
		
		 LinearLayout dir_tab_mode = (LinearLayout)findViewById(R.id.Direction_tab_mode);
	     LinearLayout dir_tab_temperature = (LinearLayout)findViewById(R.id.Direction_tab_temperature);
	     LinearLayout dir_tab_clear = (LinearLayout)findViewById(R.id.Direction_tab_clear);
	     LinearLayout dir_tab_speed = (LinearLayout)findViewById(R.id.Direction_tab_speed);
	     LinearLayout dir_tab_auxiliary = (LinearLayout)findViewById(R.id.Direction_tab_auxiliary);
	     LinearLayout dir_tab_update = (LinearLayout)findViewById(R.id.Direction_tab_update);
	     LinearLayout dir_tab_wetting = (LinearLayout)findViewById(R.id.Direction_tab_wetting);
	     LinearLayout dir_tab_timer = (LinearLayout)findViewById(R.id.Direction_tab_timer);
	     LinearLayout dir_tab_sleep = (LinearLayout)findViewById(R.id.Direction_tab_sleep);
	     LinearLayout dir_tab_surrounding = (LinearLayout)findViewById(R.id.Direction_tab_surrounding);
	     LinearLayout dir_tab_pmv = (LinearLayout)findViewById(R.id.Direction_tab_pmv);
	     LinearLayout dir_tab_silence = (LinearLayout)findViewById(R.id.Direction_tab_silence);
	     LinearLayout dir_tab_network = (LinearLayout)findViewById(R.id.Direction_tab_network);
	     
	     ArrayList<LinearLayout> arrayList =new ArrayList<LinearLayout>();
	     
	     arrayList.add(dir_tab_mode);
	     arrayList.add(dir_tab_temperature);
	     arrayList.add(dir_tab_clear);
	     arrayList.add(dir_tab_speed);
	     arrayList.add(dir_tab_auxiliary);
	     arrayList.add(dir_tab_update);
	     arrayList.add(dir_tab_wetting);
	     arrayList.add(dir_tab_timer);
	     arrayList.add(dir_tab_sleep);
	     arrayList.add(dir_tab_surrounding);
	     arrayList.add(dir_tab_pmv);
	     arrayList.add(dir_tab_silence);
	     arrayList.add(dir_tab_network);
     
	     for(LinearLayout linear : arrayList){
	    	 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(childWidth, LayoutParams.FILL_PARENT);
	    	 linear.setLayoutParams(params);
	     }
	     
	}

	public void onTabSelect(View v) {
		
		Intent intent;
		
		switch (v.getId()) {
		
		case R.id.Direction_tab_mode:
			onTabSelected(v,R.id.subTabModeView);
			
			break;
		case R.id.Direction_tab_temperature:
			intent = new Intent(this,TemperatureActivity.class);
			startActivity(intent);
			
			break;
		case R.id.Direction_tab_clear:
			intent = new Intent(this,ClearAirActivity.class);
			startActivity(intent);
			
			break;
		case R.id.Direction_tab_speed:
			intent = new Intent(this,WindySpeedActivity.class);
			startActivity(intent);
			onCloseAllSubTabs();
			break;
		case R.id.Direction_tab_auxiliary:
			
			onTabSelected(v,R.id.subTabAuxiliaryView);
			
			break;
		case R.id.Direction_tab_update:
			
			onTabSelected(v,R.id.subTabUpdateView);
			
			break;
		case R.id.Direction_tab_wetting:
			intent = new Intent(this,WettingActivity.class);
			startActivity(intent);
			onCloseAllSubTabs();
			break;
		case R.id.Direction_tab_timer:
			intent = new Intent(this,TimerActivity.class);
			startActivity(intent);
			onCloseAllSubTabs();
			break;
		case R.id.Direction_tab_sleep:
			intent = new Intent(this,SleepActivity.class);
			startActivity(intent);
			onCloseAllSubTabs();
			break;
		case R.id.Direction_tab_surrounding:
			intent = new Intent(this,SurroundingActivity.class);
			startActivity(intent);
			onCloseAllSubTabs();
			break;
		case R.id.Direction_tab_pmv:
			intent = new Intent(this,PMVActivity.class);
			startActivity(intent);
			onCloseAllSubTabs();
			break;
		case R.id.Direction_tab_silence:
			
			onTabSelected(v, R.id.subTabSilenceView);
			
			break;
		case R.id.Direction_tab_network:
			intent = new Intent(this,NetWorkActivity.class);
			startActivity(intent);
			break;
			
		}
		
	}
	
	private void onTabSelected(View v,int subTabRes){
//		onCloseAllSubTabs();
		View view = findViewById(subTabRes);
		ImageView shadowImv = (ImageView)findViewById(R.id.shadowImageView);
		
		if(view.getVisibility() == View.GONE){
			
			onCloseAllSubTabs();
			
			view.setVisibility(View.VISIBLE);
			v.setBackgroundResource(R.drawable.tab_selected);
			shadowImv.setVisibility(View.VISIBLE);
		}else{
			view.setVisibility(View.GONE);
			v.setBackgroundResource(R.drawable.transprent);
			shadowImv.setVisibility(View.GONE);
		}
		
	}
	
	public void onShadowTouched(View v){
		v.setVisibility(View.GONE);
		onCloseAllSubTabs();
	}
	
	private void onCloseAllSubTabs(){
		
		findViewById(R.id.subTabModeView).setVisibility(View.GONE);
		findViewById(R.id.subTabAuxiliaryView).setVisibility(View.GONE);
		findViewById(R.id.subTabUpdateView).setVisibility(View.GONE);
		findViewById(R.id.subTabSilenceView).setVisibility(View.GONE);
		
		findViewById(R.id.Direction_tab_mode).setBackgroundResource(R.drawable.transprent);
		findViewById(R.id.Direction_tab_auxiliary).setBackgroundResource(R.drawable.transprent);
		findViewById(R.id.Direction_tab_update).setBackgroundResource(R.drawable.transprent);
		findViewById(R.id.Direction_tab_silence).setBackgroundResource(R.drawable.transprent);
		
		findViewById(R.id.shadowImageView).setVisibility(View.GONE);
	}	
	
	public void resetViews(){
		
		Mode mode = new GreeService(this).getModePreference();
		
		findViewById(R.id.modeZhireView).setVisibility(View.GONE);
		findViewById(R.id.modeZhilengView).setVisibility(View.GONE);
		findViewById(R.id.modeSongfengView).setVisibility(View.GONE);
		findViewById(R.id.modeChushiView).setVisibility(View.GONE);
		
		ImageView firstNumImv = (ImageView) findViewById(R.id.fisrtNumberImageView);
		ImageView secondNumImv = (ImageView) findViewById(R.id.secondNumberImageView);
		ImageView unitImv = (ImageView) findViewById(R.id.unitImageView);
		
		if (mode.getZhire()) {
			ModeZhireView modeZhireView = (ModeZhireView) findViewById(R.id.modeZhireView);
			modeZhireView.setVisibility(View.VISIBLE);
			modeZhireView.startZhireAnimation();
		}
		if (mode.getZhileng()) {

			ModeZhilengView modeZhilengView = (ModeZhilengView) findViewById(R.id.modeZhilengView);
			modeZhilengView.setVisibility(View.VISIBLE);
			modeZhilengView.startZhireAnimation();
		}
		if (mode.getSongfeng()) {

			ModeSongfengView modeSongfengView = (ModeSongfengView) findViewById(R.id.modeSongfengView);
			modeSongfengView.setVisibility(View.VISIBLE);
			modeSongfengView.startSongfengAnimation();
		}
		if (mode.getChushi()) {
			ModeChushiView modeChushiView = (ModeChushiView) findViewById(R.id.modeChushiView);
			modeChushiView.setVisibility(View.VISIBLE);
			modeChushiView.startChushiAnimation();
		}
		
		setTemperature(mode);

	}
	
	private void setTemperature(Mode mode){
		int[] numImvs = {
				 R.drawable.zero,
				 R.drawable.one,
				 R.drawable.two,
				 R.drawable.three,
				 R.drawable.four,
				 R.drawable.five,
				 R.drawable.six,
				 R.drawable.seven,
				 R.drawable.eight,
				 R.drawable.nine,
				};
       
       ImageView firstNumImv   = (ImageView)findViewById(R.id.fisrtNumberImageView);
       ImageView secondNumImv  = (ImageView)findViewById(R.id.secondNumberImageView);
       ImageView unitImv 	    = (ImageView)findViewById(R.id.unitImageView);
       
       if(mode.getAuto()){
			firstNumImv.setBackgroundResource(R.drawable.auto);
			secondNumImv.setBackgroundResource(0);
			unitImv.setBackgroundResource(0);
       }else{
    	   Temperature temperature = new GreeService(this).getTemperaturePreference();
           if(temperature.iscUnitKey()){
           	unitImv.setBackgroundResource(R.drawable.unit_c);
               firstNumImv.setBackgroundResource(numImvs[Math.round(temperature.getUnit_C())/10]);
               secondNumImv.setBackgroundResource(numImvs[Math.round(temperature.getUnit_C())%10]);
           }else{
               unitImv.setBackgroundResource(R.drawable.unit_f);
               firstNumImv.setBackgroundResource(numImvs[Math.round(temperature.getUnit_F())/10]);
               secondNumImv.setBackgroundResource(numImvs[Math.round(temperature.getUnit_F())%10]);
           }
       }
      
	}

}